import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { HttpClient, HttpParams } from '@angular/common/http';
import { BaseHttpService } from '../../../../shared/services/baseHttp.service';
import { StorageService } from '../../../../shared/services/storage.service';
import { CompanyESGSummaryComponent } from './summary/esgSummary.component';
import { CompanyESGAnalystViewComponent } from './analystView/esgAnalystView.component';
import { EsgOverallPerformanceSummary } from './summary/models/EsgOverallPerformanceSummary.model';
import { EsgHistoricalPerformanceSummary } from './summary/models/esgHistoricalPerformanceSummary.model';
import { ResponseContentType } from '@angular/http';
import { EsgKeyIssueValue } from './analystView/models/EsgKeyIssueValue.model';
import { EsgOverallAssessment } from './analystView/models/EsgOverallAssessment.model';
import { EsgKeyIssueIndicator } from './analystView/models/EsgKeyIssueIndicator.model';
import { EsgKeyIssueAssessment } from './analystView/models/EsgKeyIssueAssessment.model';
import { EsgRelativePosition } from './summary/models/EsgRelativePosition.model';
import { IssueType } from '../../../../shared/models/issue.enum';
import { EsgRpIndicatorCluster } from './esgIssue/models/esgRpIndicatorCluster.model';
import { EsgRpIndicatorForCompany } from './esgIssue/models/esgRpIndicatorForCompany.model';
import { EsgThemeOverallPerformanceSummary } from './esgIssue/models/esgThemeOverallPerformanceSummary.model';
import { EsgIndustryLeadPersonnel } from './industryPersonnel/models/EsgIndustryLeadPersonnel.model';
import { ControversyQualitativePerformanceModel } from '../../../../shared/models/qualitativePerformance.model';

/**
 * Company esg main service
 * 
 * Author: Andrei Istrate
 * Date: 22.02.2018
 */
@Injectable()
export class CompanyEsgService extends BaseHttpService {
    constructor(public http: HttpClient) {
        super();
    }

    /**
     * Get company's ESG Overall performance
     * @param companyId Company unique identifier
     */
    public getEsgOverallPerformanceSummary(companyId: string): Observable<EsgOverallPerformanceSummary> {
        let user = StorageService.get().userInfo;
        let params: HttpParams = new HttpParams();
        params = params.set('esgProfileId', user.esgProfileId);
        return this.http.get<EsgOverallPerformanceSummary>(this.serverUrl + 'esgratingserv/api/v1/company/' + companyId + '/esgOverallPerformanceSummary', { params: params });
    }

    /**
    * Get the industry lead of a company
    * @param companyId Company unique identifier
    */
    public getEsgIndustryLeadPersonnel(companyId: string): Observable<EsgIndustryLeadPersonnel> {
        let user = StorageService.get().userInfo;
        let params: HttpParams = new HttpParams();
        return this.http.get<EsgIndustryLeadPersonnel>(this.serverUrl + 'esgratingserv/api/v1/company/' + companyId + '/esgIndustryLeadPersonnel', { params: params }).map(data => {
            if (data == null) return null;
            else {
                data.photoUrl = data ? this.serverUrl + data.photoUrl : null;
            }
            return data;
        });
    }

    /**
    * Get the image of the Research Product Manager
    */
    public getResearchProductManagerImage(): string {
        return this.serverUrl + "../../../Content/images/research_product_manager.png";;
    }

    /**
    * Get the relative position of a company
    * @param companyId Company unique identifier
    */
    public getEsgRelativePosition(companyId: string): Observable<EsgRelativePosition> {
        let user = StorageService.get().userInfo;
        let params: HttpParams = new HttpParams();
        params = params.set('esgProfileId', user.esgProfileId);
        return this.http.get<EsgRelativePosition>(this.serverUrl + 'esgratingserv/api/v1/company/' + companyId + '/esgRelativePosition', { params: params });;
    }

    /**
    * Get the historical performance of a company
    * @param companyId Company unique identifier
    */
    public getEsgHistoricalPerformanceSummary(companyId: string): Observable<EsgHistoricalPerformanceSummary> {
        let user = StorageService.get().userInfo;
        let params: HttpParams = new HttpParams();
        return this.http.get<EsgHistoricalPerformanceSummary>(this.serverUrl + 'esgratingserv/api/v1/company/' + companyId + '/esgHistoricalPerformanceSummary', { params: params });;
    }

    /**
    * Get ESG key issue values of a company
    * @param companyId Company unique identifier
    */
    public getEsgKeyIssueValue(companyId: string): Observable<EsgKeyIssueValue[]> {
        let user = StorageService.get().userInfo;
        let params: HttpParams = new HttpParams();
        params = params.set('esgProfileId', user.esgProfileId);
        return this.http.get<EsgKeyIssueValue[]>(this.serverUrl + 'esgratingserv/api/v1/company/' + companyId + '/keyEsgIssueValue', { params: params });;
    }

    /**
    * Get the ESG overall performance of a company
    * @param companyId Company unique identifier
    */
    public getEsgOverallAssessment(companyId: string): Observable<EsgOverallAssessment> {
        let user = StorageService.get().userInfo;
        let params: HttpParams = new HttpParams();
        return this.http.get<EsgOverallAssessment>(this.serverUrl + 'esgratingserv/api/v1/company/' + companyId + '/esgOverallAssessment', { params: params });;
    }

    /**
    * Get the ESG key issue indicators of a company
    * @param companyId Company unique identifier
    */
    public getEsgKeyIssueIndicator(companyId: string): Observable<EsgKeyIssueIndicator[]> {
        let user = StorageService.get().userInfo;
        let params: HttpParams = new HttpParams();
        return this.http.get<EsgKeyIssueIndicator[]>(this.serverUrl + 'esgratingserv/api/v1/company/' + companyId + '/keyEsgIssueIndicator', { params: params });;
    }

    /**
    * Get the ESG key issue indicators of a company
    * @param companyId Company unique identifier
    */
    public getEsgKeyIssueAssessment(companyId: string): Observable<EsgKeyIssueAssessment[]> {
        let user = StorageService.get().userInfo;
        let params: HttpParams = new HttpParams();
        return this.http.get<EsgKeyIssueAssessment[]>(this.serverUrl + 'esgratingserv/api/v1/company/' + companyId + '/keyEsgIssueAssessment', { params: params });;
    }

    /**
    * Get the ESG Theme Overall performance of a company
    * @param companyId Company unique identifier
    * @param theme Theme id 
    */
    public getEsgThemeOverallPerformanceSummary(companyId: string, theme: IssueType): Observable<EsgThemeOverallPerformanceSummary> {
        let user = StorageService.get().userInfo;
        let params: HttpParams = new HttpParams();
        params = params.set('esgProfileId', user.esgProfileId)
            .set('theme', theme.toString());
        return this.http.get<EsgThemeOverallPerformanceSummary>(this.serverUrl + 'esgratingserv/api/v1/company/' + companyId + '/esgThemeOverallPerformanceSummary', { params: params });;
    }

    /**
    * Get the ESG Indicator Cluster
    * @param theme Theme id 
    */
    public getEsgRpIndicatorCluster(theme: IssueType): Observable<EsgRpIndicatorCluster[]> {
        let user = StorageService.get().userInfo;
        let params: HttpParams = new HttpParams();
        params = params.set('theme', theme.toString());
        return this.http.get<EsgRpIndicatorCluster[]>(this.serverUrl + 'esgratingserv/api/v1/esgRpIndicatorCluster', { params: params });;
    }

    /**
    * Get the ESG Indicator by theme for a company
    * @param companyId Company unique identifier
    * @param theme Theme id 
    * @param clusterName Cluster Name
    * @param skip 
    * @param take  
    */
    public getEsgCompanyRpIndicator(companyId: string, theme: IssueType, clusterName: string): Observable<EsgRpIndicatorForCompany[]> {
        let user = StorageService.get().userInfo;
        let params: HttpParams = new HttpParams();
        params = params.set('esgProfileId', user.esgProfileId)
            .set('clusterName', clusterName)
            .set('theme', theme.toString())
            .set('indicatorNumber', "");
        return this.http.get<EsgRpIndicatorForCompany[]>(this.serverUrl + 'esgratingserv/api/v1/company/' + companyId + '/esgRpIndicator', { params: params });
    }

    /**
     * Get company's controversy performance
     * @param companyId Company unique identifier 
     */
    public getControversyQualitativePerformance(companyId: string): Observable<ControversyQualitativePerformanceModel[]> {
        let params: HttpParams = new HttpParams();
        params = params.set('qualitativeperformance', 'true');
        return this.http.get<ControversyQualitativePerformanceModel[]>(this.serverUrl + 'controversiesserv/api/v1/company/' + companyId + '/controversyIndicator/' + companyId, { params: params });
    }

    public getEsgThresholds(peerGroup: string, theme?: IssueType): Observable<any[]> {
        return this.http.get<any[]>(this.serverUrl + 'esgratingserv/api/v1/company/esgThresholds/' + peerGroup
            + '/' + (theme || 'Overall'));
    }

    public getWeightMatrices(): Observable<any[]> {
        let userInfo = StorageService.get().userInfo;
        let params: HttpParams = new HttpParams();
        params = params.set('accountId', userInfo.accountId);
        return this.http.get<any[]>(this.serverUrl + '/esgratingserv/api/v1/esgratings/weightmatrices', { params: params });
    }

    public getReferenceUniverses(): Observable<any[]> {
        return this.http.get<any[]>(this.serverUrl + '/esgratingserv/api/v1/esgratings/referenceUniverses');
    }
}